from typing import Any, Awaitable, Callable, Dict

from aiogram import BaseMiddleware, Bot
from aiogram.types import Message

from utils import alert_admin, log


class IsBotMiddleware(BaseMiddleware): 
    def __init__(self, bot: Bot) -> None:
        self.bot = bot
        super().__init__()

    async def __call__(
        self,
        handler: Callable[[Message, Dict[str, Any]], Awaitable[Any]],
        event: Message,
        data: Dict[str, Any],
    ) -> Any:
        target = event.from_user
        if target.is_bot:
            text = f"Bot detected @{target.username} ({target.id})"
            log.warning(text)
            await alert_admin(self.bot, text)
            return
        else:
            return await handler(event, data)
